package com.zql.LeetCode;

/**
 * Created By ShuHua on 2021/9/23.
 */
public class Search_704 {
    /**
     * [-1,0,3,5,9,12] 9  result 4
     * @param nums
     * @param target
     * @return
     */
    public static int search(int[] nums, int target) {
        if(nums.length==0) return -1;
        int left=0;
        int right=nums.length-1;
        int mid=0;
        while (left<=right){
            mid=(left+right)/2;
            if(nums[mid]==target){
                return mid;
            }else if(nums[mid]<target){
                left=mid+1;
            }else if(nums[mid]>target){
                right=mid-1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] nums = {-1,0,3,5,9,12};
        int result = search(nums,2);
        System.out.println(result);

    }
}
